package lxs.swift.instance.enums;

import com.alibaba.fastjson2.JSON;
import lxs.swift.tool.Dater;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.temporal.Temporal;
import java.util.Collection;
import java.util.Date;
import java.util.Objects;


public enum ClassEnum {

    INT, LONG, DOUBLE, CHAR, BOOLEAN, DATE, STRING, LIST, OBJECT, ENUM;

    public static ClassEnum strictClassEnum(Object instance) {
        Objects.requireNonNull(instance);
        if (instance.getClass().isEnum()) return ENUM;
        if (instance instanceof CharSequence) {
            return STRING;
        }
        if (instance instanceof Boolean) {
            return BOOLEAN;
        }
        if (instance instanceof Character) {
            return CHAR;
        }
        if (instance instanceof Byte || instance instanceof Short || instance instanceof Integer) {
            return INT;
        }
        if (instance instanceof Long || instance instanceof BigInteger) {
            return LONG;
        }
        if (instance instanceof Float || instance instanceof Double || instance instanceof BigDecimal) {
            return DOUBLE;
        }
        if (instance instanceof Temporal || instance instanceof Date) {
            return DATE;
        }
        if (instance instanceof Collection || instance.getClass().isArray()) {
            return LIST;
        }
        return OBJECT;
    }

    public static ClassEnum similarClassEnum(Object instance) {
        Objects.requireNonNull(instance);
        if (instance.getClass().isEnum()) return ENUM;
        if (instance instanceof Collection || instance.getClass().isArray()) {
            return LIST;
        }
        if (instance instanceof Byte || instance instanceof Short || instance instanceof Integer) {
            return INT;
        }
        if (instance instanceof Character) {
            char character = (Character) instance;
            if (character >= '0' && character <= '9') {
                return INT;
            }
            return CHAR;
        }
        if (instance instanceof Long || instance instanceof BigInteger) {
            return LONG;
        }
        if (instance instanceof Float || instance instanceof Double || instance instanceof BigDecimal) {
            return DOUBLE;
        }
        if (instance instanceof Date || instance instanceof Temporal) {
            return DATE;
        }
        if (instance instanceof CharSequence) {
            CharSequence sequence = (CharSequence) instance;
            String s = sequence.toString();
            if (s.length() == 1) {
                char character = s.charAt(0);
                if (character >= '0' && character <= '9') {
                    return INT;
                }
                return CHAR;
            }
            if (s.isEmpty()) return STRING;
            if (s.matches("-?[0-9]+")) {
                try {
                    long l = Long.parseLong(s);
                    if (l <= Integer.MAX_VALUE && l >= Integer.MIN_VALUE) return INT;
                    return LONG;
                } catch (NumberFormatException e) {
                    return STRING;
                }
            }
            if (s.matches("-?[0-9]+\\.[0-9]+")) {
                try {
                    Double.parseDouble(s);
                    return DOUBLE;
                } catch (NumberFormatException e) {
                    return STRING;
                }
            }
            if (s.equals("[]")) return LIST;
            Date parse = Dater.parse(s).date;
            if (parse != null) return DATE;
            try {
                JSON.parseArray(instance.toString());
                return LIST;
            } catch (Exception e1) {
                return STRING;
            }
        }
        return OBJECT;
    }

    public static ClassEnum classEnum(Class<?> aClass) {
        Objects.requireNonNull(aClass);
        if (aClass.isEnum()) return ENUM;
        if (aClass.isArray()) return LIST;
        if (Collection.class.isAssignableFrom(aClass)) return LIST;
        if (CharSequence.class.isAssignableFrom(aClass)) return STRING;
        if (aClass.equals(Character.class) || aClass.equals(char.class)) return CHAR;
        if (aClass.equals(Boolean.class) || aClass.equals(boolean.class)) return BOOLEAN;
        if (Collection.class.isAssignableFrom(aClass) || Collection.class.isAssignableFrom(aClass)) return DATE;
        if (aClass.equals(Byte.class) || aClass.equals(Short.class) || aClass.equals(Integer.class)) return INT;
        if (aClass.equals(byte.class) || aClass.equals(short.class) || aClass.equals(int.class)) return INT;
        if (aClass.equals(Long.class) || aClass.equals(long.class) || aClass.equals(BigInteger.class)) return LONG;
        if (aClass.equals(Double.class) || aClass.equals(Float.class) || aClass.equals(BigDecimal.class) || aClass.equals(double.class) || aClass.equals(float.class))
            return DOUBLE;
        return OBJECT;
    }

    public static ClassEnum classEnum(int ordinal) {
        switch (ordinal) {
            case 0:
                return INT;
            case 1:
                return LONG;
            case 2:
                return DOUBLE;
            case 3:
                return CHAR;
            case 4:
                return BOOLEAN;
            case 5:
                return DATE;
            case 6:
                return STRING;
            case 7:
                return LIST;
            case 8:
                return OBJECT;
            case 9:
                return ENUM;
            default: {
                throw new IllegalArgumentException(String.valueOf(ordinal));
            }
        }
    }

}
